#include <cstdio>
#include <cstdlib>
#include <string>
#include "date.h"	

using std::string;

namespace
{
	const int SIZE_BUFFER = 25;
	const int SIZE_YEAR = 5;
	const int SIZE_DAY = 3;
	const int SIZE_HOUR = 3;
	const int SIZE_MONTH = 3;
	const int SIZE_MINUTE = 3;
	const int SIZE_SECOND = 3;
	const string YEAR_FORMAT("YYYY");
	const string MONTH_SHORT_FORMAT("MM");
	const string MONTH_LONG_FORMAT("MMM");
	const string DAY_FORMAT("DD");
	const string HOUR_FORMAT("hh");
	const string MINUTE_FORMAT("mm");
	const string SECOND_FORMAT("ss");
}

namespace date
{
	int classDate::getYear() 
	{
		return year;
	}

	int classDate::getMonth() 
	{
		return month;
	}

	int classDate::getDay() 
	{
		return day;
	}

	int classDate::getHour() 
	{
		return hour;
	}

	int classDate::getMinute() 
	{
		return minute;
	}

	int classDate::getSecond() 
	{
		return second;
	}

	string classDate::toString() 
	{
		static const char* strMonth[MAX_MONTH] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

		char buffer[SIZE_BUFFER] = {0};
		


	}

	string classDate::formatDate(const string format) const
	{
		static const char* strMonth[MAX_MONTH] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
		char strYear[SIZE_YEAR] = {0};
		char strNumbMonth[SIZE_MONTH] = {0};
		char strDay[SIZE_DAY] = {0};
		char strHour[SIZE_HOUR] = {0};
		char strMinute[SIZE_MINUTE] = {0};
		char strSecond[SIZE_SECOND] = {0};

		_snprintf_s(strYear, SIZE_YEAR, "%04d", year);
		_snprintf_s(strNumbMonth, SIZE_MONTH, "%02d", static_cast<int>(month));
		_snprintf_s(strDay, SIZE_DAY, "%02d", day);
		_snprintf_s(strHour, SIZE_HOUR, "%02d", hour);
		_snprintf_s(strMinute, SIZE_MINUTE, "%02d", minute);
		_snprintf_s(strSecond, SIZE_SECOND, "%02d", second);

		string date(format);

		size_t found = 0;
		while (string::npos != (found = date.find(YEAR_FORMAT)))
		{
			date.replace(found, YEAR_FORMAT.size(), strYear);
		}

		while (string::npos != (found = date.find(MONTH_LONG_FORMAT)))
		{
			date.replace(found, MONTH_LONG_FORMAT.size(), strMonth[static_cast<int>(month)-1]);
		}

		while (string::npos != (found = date.find(MONTH_SHORT_FORMAT)))
		{
			date.replace(found, MONTH_SHORT_FORMAT.size(), strNumbMonth);
		}

		while (string::npos != (found = date.find(DAY_FORMAT)))
		{
			date.replace(found, DAY_FORMAT.size(), strDay);
		}

		while (string::npos != (found = date.find(HOUR_FORMAT)))
		{
			date.replace(found, HOUR_FORMAT.size(), strHour);
		}

		while (string::npos != (found = date.find(MINUTE_FORMAT)))
		{
			date.replace(found, MINUTE_FORMAT.size(), strMinute);
		}

		while (string::npos != (found = date.find(SECOND_FORMAT)))
		{
			date.replace(found, SECOND_FORMAT.size(), strSecond);
		}

		return date;
	}

}
